package com.xiaoshuidi.cloud.module.contract.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractSmsConfigDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.bill.RentOrderMapper;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractSmsConfigMapper;
import com.xiaoshuidi.cloud.module.system.api.sms.SmsSendApi;
import com.xiaoshuidi.cloud.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
@TenantJob
public class SmsMessageBillJob {

    @Resource
    private ContractSmsConfigMapper contractSmsConfigMapper;
    @Resource
    private RentOrderMapper rentOrderMapper;
    @Resource
    private SmsSendApi smsSendApi;

    @XxlJob("SmsMessageBillJob")
    @TenantIgnore
    public void execute(){
        log.info("SmsMessageBillJob催款短信推送开始");
        //查询配置
        List<ContractSmsConfigDO> contractSmsConfigDOS = contractSmsConfigMapper.selectList();
        if(CollectionUtil.isNotEmpty(contractSmsConfigDOS)){
            contractSmsConfigDOS.forEach(item->{
                Long apartmentId = item.getApartmentId();
                QueryWrapper<RentOrderDO> wrapper = Wrappers.query();
                wrapper.eq("pay_status","unpaid");
                wrapper.eq("deleted","0");
                wrapper.eq("deadline_pay_time", DateUtil.format(DateUtil.offsetDay(new Date(),Integer.valueOf("-" + item.getBillPushDay())),"yyyy-MM-dd"));
                wrapper.eq("apartment_id", apartmentId);
                List<RentOrderDO> rentOrderDOS = rentOrderMapper.selectList(wrapper);
                //存在
                if(CollectionUtil.isNotEmpty(rentOrderDOS)){
                    rentOrderDOS.forEach(itemOrder->{
                        Map<String, Object> map =  new HashMap<>();
                        // 获得渠道对应的 SmsClient 客户端
                        SmsSendSingleToUserReqDTO smsSendSingleToUserReqDTO = new SmsSendSingleToUserReqDTO();
                        //查询是否企业用户
                        if (itemOrder.getRoommateId() == 0) {
                            map.put("begin",itemOrder.getStartTime().format(DateTimeFormatter.ISO_DATE));
                            map.put("end",itemOrder.getEndTime().format(DateTimeFormatter.ISO_DATE));
                            map.put("apartmentName",itemOrder.getApartmentName());
                            map.put("roomName",itemOrder.getRoomName());
                            map.put("amount",itemOrder.getTotalAmount().subtract(itemOrder.getPaidRentUtilities()));
                            //短信渠道
                            smsSendSingleToUserReqDTO.setTemplateCode("order_sms_collection");
                        }else {
                            map.put("begin",itemOrder.getStartTime().format(DateTimeFormatter.ISO_DATE));
                            map.put("end",itemOrder.getEndTime().format(DateTimeFormatter.ISO_DATE));
                            map.put("amount",itemOrder.getTotalAmount().subtract(itemOrder.getPaidRentUtilities()));
                            //短信渠道
                            smsSendSingleToUserReqDTO.setTemplateCode("company_order_sms_collection");
                        }
                        smsSendSingleToUserReqDTO.setMobile(itemOrder.getCustomerPhone());
                        smsSendSingleToUserReqDTO.setUserId(itemOrder.getMemberId());
                        smsSendSingleToUserReqDTO.setTemplateParams(map);
                        smsSendApi.sendSingleSmsToMember(smsSendSingleToUserReqDTO);
                    });
                }
            });
        }
    }
}
